#include "init.h"
#include "display.h"
#include "button.h"
#include "motor.h"


void init_osc(void)
{
	//MCLK = SMCLK = 1 Mhz
	WDTCTL = WDTPW +WDTHOLD;                  // Stop Watchdog Timer
	if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF)                                     
	{  
		while(1);                               // If calibration constants erased
                                            // do not load, trap CPU!!
	} 
	//1Mhz
	BCSCTL1 = CALBC1_16MHZ;                    // Set range
	DCOCTL = CALDCO_16MHZ;                     // Set DCO step + modulation */
}

void init_IO(void)
{
	//set outputs for LED7Seg and motor
	P2DIR |= (1<<LED_PIN_D)|(1<<LED_PIN_E)|(1<<LED_PIN_F)|(1<<LED_CHOOSE_LED1)|(1<<LED_CHOOSE_LED2)|(1<<LED_CHOOSE_LED3);
	P3DIR |= (1<<MOTOR)|(1<<LED_PIN_A)|(1<<LED_PIN_B)|(1<<LED_PIN_C)|(1<<LED_PIN_G)|(1<<LED_PIN_DP)|(1<<LED_CHOOSE_LED4);
	
	//P3SEL |= 1<<MOTOR;
	motor_off();
	
	//set inputs for buttons
	P1DIR &= ~((1<<BUTTON_SPEED_UP)|(1<<BUTTON_SPEED_DOWN)|(1<<BUTTON_TIMER_UP));
	P3DIR &= ~(1<<BUTTON_TIMER_DOWN);

	P3OUT &= ~(1<<3);
}

void init_timer(void)
{
	TACTL = TASSEL_2 + MC_1 + TAIE;           // SMCLK, up/down mode, interrupt
	CCTL2 = OUTMOD_4 + CCIE;	//Toggle
	CCTL1 = CCIE; 
	CCTL0 = CCIE;
	CCR0 = 10000;//1000
	CCR1 = 100;//500
	CCR2 = 500;

	TA1CTL = TASSEL_1 + MC_2;
	TA1CCTL0 = CCIE;
	TA1CCR0 = 32767;
	_BIS_SR(GIE);                 // Enter LPM0 w/ interrupt
}
